﻿Public Class FrmHangSanXuat
    Private selectedmahang As String


    Private Sub btnThoat_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThoat.Click
        Me.Close()
    End Sub

    Private Sub btnNhapLai_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNhapLai.Click
        PhucHoi()
        txtTenHang.Text = ""
        txtQuocGia.Text = ""
        txtWebsite.Text = ""
    End Sub

    Private Sub btnThem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnThem.Click
        PhucHoi()
        If txtQuocGia.Text <> "" And txtTenHang.Text <> "" And txtWebsite.Text <> "" Then
            If Not HangSanXuat.them(txtTenHang.Text, txtQuocGia.Text, txtWebsite.Text) Then
                MsgBox("Đã có Hãng Sản Xuất này rồi !", MsgBoxStyle.Critical, "Lỗi")
            End If
            dgvHangSanXuat.DataSource = HangSanXuat.dshsx()
            btnNhapLai_Click(sender, e)
        Else
            MsgBox("Bạn hãy nhập đầy đủ thông tin vào ! ", MsgBoxStyle.Information, "Thông báo")
        End If
        
    End Sub

    Private Sub FrmHangSanXuat_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        btnSua.Text = "&Sửa"
        dgvHangSanXuat.DataSource = HangSanXuat.dshsx()
    End Sub

    Private Sub btnSua_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSua.Click
        If btnSua.Text = "&Sửa" Then
            selectedmahang = dgvHangSanXuat.CurrentRow.Cells("MaHang").Value
            txtTenHang.Text = dgvHangSanXuat.CurrentRow.Cells("TenHang").Value
            txtQuocGia.Text = dgvHangSanXuat.CurrentRow.Cells("QuocGia").Value
            txtWebsite.Text = dgvHangSanXuat.CurrentRow.Cells("Website").Value
            btnSua.Text = "&Lưu"
        Else
            If Not HangSanXuat.capnhat(selectedmahang, txtTenHang.Text, txtQuocGia.Text, txtWebsite.Text) Then
                MsgBox("Không thể lưu dữ liệu ", MsgBoxStyle.Critical, "Lỗi")
            End If
            btnSua.Text = "&Sửa"
            dgvHangSanXuat.DataSource = HangSanXuat.dshsx()
            btnNhapLai_Click(sender, e)
        End If
    End Sub

    Private Sub btnTimKiem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTimKiem.Click
        btnSua.Text = "&Sửa"
        dgvHangSanXuat.DataSource = HangSanXuat.timkiem(txtTuKhoa.Text)

    End Sub

    Private Sub btndaydu_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndaydu.Click
        PhucHoi()
        dgvHangSanXuat.DataSource = HangSanXuat.dshsx()
    End Sub
    Private Sub PhucHoi()
        btnSua.Text = "&Sửa"
    End Sub
    Private Sub btnXoa_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXoa.Click
        btnSua.Text = "&Sửa"
        If MsgBox("Bạn chắc chắn muốn xóa hãng sản xuất này chứ?", MsgBoxStyle.YesNo, "Thông Báo") = DialogResult.Yes Then
            If Not HangSanXuat.xoa(dgvHangSanXuat.CurrentRow.Cells("MaHang").Value) Then
                MsgBox("Bạn không thể xóa hãng sản xuất này", MsgBoxStyle.Critical, "Lỗi")
            End If
            dgvHangSanXuat.DataSource = HangSanXuat.dshsx()
            btnNhapLai_Click(sender, e)
        End If
    End Sub

    Private Sub dgvHangSanXuat_CellDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvHangSanXuat.CellDoubleClick
        PhucHoi()
        btnSua_Click(sender, e)
    End Sub


End Class